/**
 * 日期处理类
 */
package com.zhekou.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    /**
     * 格式化当前日期为 'yyyyMMddHHmmss'.
     *
     * @return String  格式化后日期.
     */
    public static String getCurrTime() {
        Date now = new Date();
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return outFormat.format(now);
    }

    public static Date getBeforeDay(int days) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -days);
        return cal.getTime();
    }

    public static String getCurrTime1() {
        Date now = new Date();
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
        return outFormat.format(now);
    }

    public static String getCurrStr(Date date) {
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
        return outFormat.format(date);
    }

    public static String getCurrTime2() {
        Date now = new Date();
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return outFormat.format(now);
    }

    public static String getCurrTime2(Date date) {
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return outFormat.format(date);
    }

    /**
     * 是否为当天
     *
     * @param date date
     * @return bool
     */
    public static boolean isToday(Date date) {
        Date now = new Date();
        return org.apache.commons.lang.time.DateUtils.isSameDay(now, date);
    }

    public static boolean isToday(long seconds) {
        Date now = new Date();
        Date date = DateUtils.parseDateFromSeconds(seconds);
        return org.apache.commons.lang.time.DateUtils.isSameDay(now, date);
    }

    public static boolean isSameDay(long seconds1, long seconds2) {
        Date date1 = DateUtils.parseDateFromSeconds(seconds1);
        Date date2 = DateUtils.parseDateFromSeconds(seconds2);
        return org.apache.commons.lang.time.DateUtils.isSameDay(date1, date2);
    }


    public static long getSeconds(Date date) {
        if (date != null) {
            return date.getTime() / 1000;
        } else {
            return 0;
        }
    }

    public static long getSeconds(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month, day);
        return cal.getTimeInMillis() / 1000;
    }

    public static Date parseDateFromSeconds(long seconds) {
        return new Date(seconds * 1000);
    }

    public static String formatDate(long seconds, String pattern) {
        return new SimpleDateFormat(pattern).format(new Date(seconds * 1000));
    }

    public static String formatDate2(Date date, String pattern) {
        return new SimpleDateFormat(pattern).format(date);
    }

    /**
     * 通过生日 获得年龄
     */
    public static int getAge(long seconds) {
        Date birthday = DateUtils.parseDateFromSeconds(seconds);
        Calendar cal = Calendar.getInstance();
        cal.setTime(birthday);
        Calendar now = Calendar.getInstance();
        if (now.get(Calendar.MONTH) > cal.get(Calendar.MONTH)) {
            return now.get(Calendar.YEAR) - cal.get(Calendar.YEAR) + 1;
        } else {
            return now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
        }
    }

    /**
     * 根据输入的年月日，获得DATE类型.
     */
    public static Date getDate1(String birthday) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = formatter.parse(birthday);
        return date;
    }

    /**
     * 通过年月日获得long秒
     */
    public static long getSecones(String date) throws Exception {
        return getSeconds(getDate1(date));
    }

    public static boolean afterEncryptCookie() {
        return true;
    }

    public static void main(String[] args) {
        boolean ist = isToday(1324547098);
        System.out.println(ist);
    }
}
